package com.zyx.maildemo.service;

import com.zyx.maildemo.entity.Roles;
import com.zyx.maildemo.entity.UserRole;
import com.zyx.maildemo.entity.example.UserRoleExample;
import com.zyx.maildemo.mapper.UserRoleMapper;
import com.zyx.maildemo.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

/**
 * @ClassName: UserRoleServiceImpl
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2019/7/27 13:15
 */
@Service
public class UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Transactional
    public List<UserRole> addBatch(List<UserRole> userRoles) {
        if (!CollectionUtils.isEmpty(userRoles)) {
            for (UserRole userRole : userRoles) {
                if (userRoleMapper.insertSelective(userRole) <= 0)
                    throw new RuntimeException("事务发生异常!");
            }
            return userRoles;
        }
        return null;
    }

    @Transactional
    public UserRole add(UserRole userRole) {
        if (userRole != null) {
            int i = userRoleMapper.insertSelective(userRole);
            if (i <= 0) return null;
        }
        return userRole;
    }

    @Transactional
    public UserRole delete(UserRole userRole) {
        UserRoleExample example = createExample(userRole);
        UserRole one = findOne(example);
        if (one != null) {
            if (userRoleMapper.deleteByExample(example) > 0) {
                return one;
            }
        }
        return null;
    }

    public List<Roles> getRolesByUid(long uid) {
        return userRoleMapper.selectRolesByUserId(uid);
    }


    public UserRole findOne(UserRoleExample example) {
        List<UserRole> roles = findAll(example).get();
        if (!CollectionUtils.isEmpty(roles)) {
            return roles.get(0);
        }
        return null;
    }

    public Optional<List<UserRole>> findAll(UserRoleExample example) {
        List<UserRole> userRoles = userRoleMapper.selectByExample(example);
        return Optional.ofNullable(userRoles);
    }

    /**
     * 判断角色是不是被使用
     * @param rid 角色id
     * @return 是否
     */
    public boolean roleIsUse(Integer rid) {
        UserRoleExample example = new UserRoleExample();
        example.createCriteria().andRoleIdEqualTo(rid);
        return findOne(example) != null;
    }

    private UserRoleExample createExample(UserRole userRole) {
        UserRoleExample example = new UserRoleExample();
        UserRoleExample.Criteria criteria = example.createCriteria();
        if (!NumberUtils.isZero(userRole.getUserId())) criteria.andUserIdEqualTo(userRole.getUserId());
        if (!NumberUtils.isZero(userRole.getRoleId())) criteria.andRoleIdEqualTo(userRole.getRoleId());
        if (!NumberUtils.isZero(userRole.getId())) criteria.andIdEqualTo(userRole.getId());

        return example;
    }
}
